<?php
namespace app\bbs\controller;
use x\Xbase;
use EasyWeChat\Kernel\Messages\Image;
class Wx extends Xbase{
    /**
    * 微信公众号入口
    **/
    public function init(){
        $app = m('Weixin','c')->getApp(c('api.wx_gzh_config')['myb']);
        #服务器验证
        // $response = $app->server->serve();
        // $response->send();exit;
        //用户向公众号触发行为
        $app->server->push(function ($message) use($app) {
          log_test('wx',$message);
          $message['EventKey'] = str_replace("qrscene_","", $message['EventKey']);
          switch ($message['MsgType']) {
                case 'event':
                    switch ($message['Event']) {
                      case 'subscribe':#关注
                      case 'SCAN':#扫带参数的二维码，且已关注
                         $user = $app->user->get($message['FromUserName']);
                         $user_id = $this->_subscribe($user, $message);#注册
                         //pc_login
                         $rs = $this->db->query("update pc_login set status=1, user_id=? where order_no=?",[$user_id, $message['EventKey']]);
                         if($rs){
                           return '登录网页成功!';
                         }
                         return '欢迎, 关注蚂蚁帮在线应用!';
                         break;
                      case 'unsubscribe':#取消关注
                         break;
                      case 'CLICK': #点击菜单
                          return m('userwx','c')->click($message);
                          break;
                      default:
                        // code...
                        break;
                    }
                    break;
                case 'text':
                    return m('userwx','c')->text($message);
                    return '收到文字消息';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
            // ...
        });
        $response = $app->server->serve();
        $response->send();
    }

    /**
    * 关注事件
    */
    public function _subscribe($user, $message=[]){
      $rs = $this->db->query("select id from user where wx_openid=?",[$user['openid']],1);
      $data = [
          'wx_openid'=> $user['openid'],
          'username' => $user['nickname'],
          'pic'      => $user['headimgurl'],
          'city'     => $user['city'],
          'province' => $user['province'],
      ];
      //未注册
      if(!$rs){
          //上级推荐人
          if(isset($message['EventKey'])){
            $rs = $this->db->query("select a.tj_user_id,b.s_tree from pc_login a left join user b on a.tj_user_id=b.id where a.order_no=?", [$message['EventKey']],1);
            if($rs && $rs['tj_user_id']){
              $data['s_id'] = $rs['tj_user_id'];
              $data['s_tree'] = $rs['s_tree'].$rs['tj_user_id'].",";
            }else{
              $rs = $this->db->find("user", $message['EventKey']);
              if($rs){
                  $data['s_id'] = $rs['id'];
                  $data['s_tree'] = $rs['s_tree'].$rs['id'].",";
              }else{
                  $data['s_id'] = 1;
                  $data['s_tree'] = ",0,1,";
              }
            }
          }
          $data['s_level'] = count(explode(',',$data['s_tree'])) - 2;
          $data['reg_ip'] = ip();
          $data['create_time'] = time();
          $id = $this->db->insert('user',$data);
          return $id;
      //已注册
      }else{
          $data['id'] = $rs['id'];
          $this->db->update('user',$data);
          return $rs['id'];
      }
    }

    //程序直接编辑菜单
    public function editMenu(){
        $app = m('Weixin','c')->getApp();
        $buttons = [
            // [
            //     "type" => "view",
            //     "name" => "淘宝券",
            //     "url"  => "http://www.mayibang.co/tbk/index.html"
            // ],
            // [
            //     "type" => "click",
            //     "name" => "我的海报",
            //     "key"  => "wdhb"
            // ]
            [
                "name"       => "蚂蚁项目",
                "sub_button" => [
                    [
                        "type" => "click",
                        "name" => "在线文案",
                        "key"  => "zxwa"
                    ],
                    [
                        "type" => "click",
                        "name" => "淘宝客",
                        "key"  => "tbq"
                    ],
                    [
                        "type" => "click",
                        "name" => "微信名片",
                        "key"  => "wxmp"
                    ],
                    [
                        "type" => "click",
                        "name" => "宣传单页",
                        "key"  => "xcdy"
                    ],
                ],
            ],
            [
                "type" => "click",
                "name" => "联系客服",
                "key"  => "lxkf"
            ],
            [
                "name"       => "我的",
                "sub_button" => [
                    [
                        "type" => "view",
                        "name" => "个人中心",
                        "url"  => "https://www.mayibang.co/vuser/index.html"
                    ],
                    [
                        "type" => "click",
                        "name" => "我要推广",
                        "key"  => "wytg"
                    ],
                ],
            ],
        ];
        $app->menu->create($buttons);

        // $app->menu->delete(); // 全部
        // $app->menu->delete($menuId);
        t('end');
    }
}
